package com.jiguangssp.addemo.act;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;

import com.jiguangssp.addemo.R;
import com.jiguangssp.addemo.adapter.NativeListAdapter;
import com.jiguangssp.addemo.entity.AdData;
import com.jiguangssp.addemo.util.Constants;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import xyz.adscope.amps.ad.nativead.AMPSNativeAd;
import xyz.adscope.amps.ad.nativead.AMPSNativeLoadEventListener;
import xyz.adscope.amps.ad.nativead.inter.AMPSNativeAdExpressInfo;
import xyz.adscope.amps.ad.unified.AMPSUnifiedNativeAd;
import xyz.adscope.amps.ad.unified.AMPSUnifiedNativeLoadEventListener;
import xyz.adscope.amps.ad.unified.inter.AMPSUnifiedNativeItem;
import xyz.adscope.amps.common.AMPSError;
import xyz.adscope.amps.config.AMPSRequestParameters;
import xyz.adscope.amps.tool.util.AMPSScreenUtil;

public class NativeAdActivity extends BaseAdActivity {

    List<AdData> adDataList = new ArrayList<>();
    RecyclerView recyclerView;
    NativeListAdapter nativeListAdapter;
    AMPSUnifiedNativeAd mUnifiedNativeAd;

    AMPSNativeAd ampsNativeAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_native_ad);
        recyclerView = findViewById(R.id.rv_act_native_list);
        for (int i = 0; i < 10; i++) {
            adDataList.add(new AdData(String.valueOf(new Random().nextInt())));
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        nativeListAdapter = new NativeListAdapter(adDataList);
        recyclerView.setAdapter(nativeListAdapter);
        loadAd();
    }

    @Override
    public void loadAd() {
        super.loadAd();

        boolean isUnifiedAd = false;
        if (isUnifiedAd) {
            AMPSRequestParameters parameter = new AMPSRequestParameters.Builder()
                    .setSpaceId(Constants.JG_PUB_SPACE_ID_NATIVE)//原生广告位id
                    .setTimeOut(Constants.JG_PUB_TIME_OUT)//设置超时时间
                    .setWidth(AMPSScreenUtil.getScreenWidth(this))//设置广告的宽，单位px
                    .setHeight(0)//设置广告的高，单位px,0代表自适应高度
                    .setAdCount(3)//设置请求原生广告的条数
                    .build();

            mUnifiedNativeAd = new AMPSUnifiedNativeAd(this, parameter, new AMPSUnifiedNativeLoadEventListener() {
                @Override
                public void onAmpsAdLoad(List<AMPSUnifiedNativeItem> list) {
                    if (list==null) {
                        return;
                    }
                    if (list == null || list.size()==0) {
                        return;
                    }
                    for (AMPSUnifiedNativeItem nativeItem : list) {
                        if (nativeItem == null) {
                            continue;
                        }
                        if (!nativeItem.isValid()) {
                            continue;
                        }
                        adDataList.add(new AdData(nativeItem,"ad"));
                    }
                    nativeListAdapter.notifyDataSetChanged();
                }

                @Override
                public void onAmpsAdFailed(AMPSError ampsError) {
                }
            });
            mUnifiedNativeAd.loadAd();
        }else {
            AMPSRequestParameters parameter = new AMPSRequestParameters.Builder()
                    .setSpaceId(Constants.JG_PUB_SPACE_ID_NATIVE)//原生广告位id
                    .setTimeOut(5000)//设置超时时间
                    .setWidth(AMPSScreenUtil.getScreenWidth(this))//设置广告的宽，单位px
                    .setHeight(0)//设置广告的高，单位px,0代表自适应高度
                    .setAdCount(1)//设置请求原生广告的条数
                    .build();
            ampsNativeAd = new AMPSNativeAd(this, parameter, new AMPSNativeLoadEventListener() {
                @Override
                public void onAmpsAdLoad(List<AMPSNativeAdExpressInfo> list) {
                    Log.e("JgPuAdNativeAdapter", "onAmpsAdLoad: " );
                    if (list.size()>0) {
                        for (int i = 0; i < list.size(); i++) {
                            adDataList.add(new AdData(list.get(i),"ad"));
                        }
                    }
                    nativeListAdapter.notifyDataSetChanged();
                }

                @Override
                public void onAmpsAdFailed(AMPSError ampsError) {
                    Log.e("JgPuAdNativeAdapter", ": "+ampsError.toString() );
                }
            });
            ampsNativeAd.loadAd();
        }


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mUnifiedNativeAd != null) {
            mUnifiedNativeAd.resume();
        }
        for (AdData adData : adDataList) {
            if (adData.getAdInfo() != null) {
                if (adData.getAdInfo() instanceof  AMPSUnifiedNativeItem){
                    ((AMPSUnifiedNativeItem) adData.getAdInfo()).resume();
                }

            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mUnifiedNativeAd != null) {
            mUnifiedNativeAd.pause();
        }
        for (AdData adData : adDataList) {
            if (adData.getAdInfo() != null) {
                if (adData.getAdInfo() instanceof  AMPSUnifiedNativeItem){
                    ((AMPSUnifiedNativeItem) adData.getAdInfo()).pause();
                }

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnifiedNativeAd != null) {
            mUnifiedNativeAd.destroy();
            mUnifiedNativeAd = null;
        }
        for (AdData adData : adDataList) {
            if (adData.getAdInfo() != null) {
                if (adData.getAdInfo() instanceof  AMPSUnifiedNativeItem){
                    ((AMPSUnifiedNativeItem) adData.getAdInfo()).destroy();
                }

            }
        }
    }
}